package config

import (
	"gopkg.in/yaml.v2"
	"os"
)

type Config struct {
	Oauth2 Oauth2 `json:"oauth2" yaml:"oauth2"`
	Sync   Sync   `json:"sync" yaml:"sync"`
}

type Oauth2 struct {
	RedirectURL  string   `json:"redirect_url" yaml:"redirect_url"`
	ClientId     string   `json:"client_id" yaml:"client_id"`
	Tenant       string   `json:"tenant" yaml:"tenant"`
	ClientSecret string   `json:"client_secret" yaml:"client_secret"`
	Scopes       []string `json:"scopes" yaml:"scopes"`
	Username     string   `json:"username" yaml:"username"`
	Password     string   `json:"password" yaml:"password"`
}

type Sync struct {
	Filename   string `json:"filename" yaml:"filename"`
	GoRoutines int    `json:"goroutines" yaml:"goroutines"`
}

func FromFile(filename string) (Config, error) {

	var config = Config{}
	f, err := os.OpenFile(filename, os.O_RDONLY, 0755)
	if err != nil {
		return Config{}, err
	}
	defer f.Close()

	err = yaml.NewDecoder(f).Decode(&config)
	return config, err
}
